<?php
/**
 * Created by PhpStorm.
 * User: function
 * Date: 2018-04-16
 * Time: 9:28
 */

namespace app\seller\controller;

use app\common\controller\Seller;

use app\common\model\Member;
use app\common\model\Company;
use app\common\model\Amount;
use app\common\model\Withdraw;
use think\Session;
use think\Cookie;
use app\common\model\City;
class User extends Seller
{
//    protected $authenticate =['only'=>'fdsfsda'];
    protected $authenticate =['except'=>'login'];
    
    protected $phone_login_rule = [
        'phone|手机号'  =>  'require',
        'password|密码' =>  'require',
//        'code|验证码' =>  'require',
//        'validate_token|验证码标识' =>  'require',
    ];
    
    public function edit($id=null){
        $seller = Company::get(['user_id'=>$this->getSellerId()]);
        if(!$seller){
            $this->error('商家不存在','/index');
        }
        $this->assign('provienceList',City::all(['parent_id'=>22]));
        $this->assign('seller',$seller);
        return $this->fetch();
    }
    
    public function update($id)
    {
        $params = paramForm();
        $seller = new Company();
        
        $thumb = request()->file('thumb');
        if($thumb){
            $params['thumb']=$this->uploadToAlyun($thumb);
        }

        $rt = $seller->allowField(true)->save($params,['id'=>$id]);
       
        if($rt){
            $this->success('修改成功','user/edit');
        }else{
            $this->error('修改失败,你可能没有做出更改','user/edit');
        }
    }
    
    
    public function login()
    {
        if(is_login()){
            $this->success('你已经登录过了！','/');
        }
        if($_POST){
            $params = paramForm();
            $member = Member::get(['phone'=>$params['phone']]);
    
            if(!$member){
                $this->error('商户不存在');
            }
    
            $password = base64_encode(hash('sha256', ($params['password'].$member['passsalt']),true));
            if($password!=$member['password']){
                $this->error('商户账号或者密码不符');
            }
    
            if($member['co_id']){
                session('ke_seller_auth',$member);
                $this->success('登陆成功',get_redirect_url());
            }else{
                $this->error('该用户还不是商户');
            }
            
        }
        
        $this->view->engine->layout(false);
        return $this->fetch();
    }
    
    
    public function logout(){
        Session::delete('ke_seller_auth');
        Cookie::delete('seller_redirect_url');
        $this->success('退出成功','/user/login');
    }
    
    
    /**
     * 余额变动列表
     */
    public function amountRecord(){
        
        
        $pararms = paramForm();
        $sellerId = $this->getSellerId();
        $seller = Member::get($sellerId);
        $filter = [
            'user_id'=>$seller->id,
        ];
        
        if(isset($pararms['action'])){
            
            switch ($pararms['action']){
                case 'plus':
                    $filter['action'] = 1;
                    break;
                case 'minus':
                    $filter['action'] = -1;
                    break;
                default:
                    //啥也不干
                    break;
            }
        }
        
        $list = Amount::where($filter)->order('create_time desc')->paginate(20);
        success($list);
        
    }
    
    /**
     * 提现列表
     */
    public function withdrawRecord(){
        
        
        $pararms = paramForm();
        $sellerId = $this->getSellerId();
        $seller = Member::get($sellerId);
        $filter = ['user_id'=>$seller->id];
        
        $list = Withdraw::where($filter)->paginate(20);
        success($list);
        
    }
    
    
    
}